package com.shisfish.english.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.shisfish.core.common.GlobalVariable;
import com.shisfish.core.controller.BaseController;
import com.shisfish.core.dto.PageData;
import com.shisfish.core.dto.ResultDto;
import com.shisfish.english.model.SingleWord;

@Controller
@RequestMapping(value = "/singleWord", produces = "application/json; charset=utf-8")
public class SingleWordController extends BaseController {

	
	@ResponseBody
	@RequestMapping(value = "/add")
	public String add(String name, String meaning) {
		if (name == null && meaning == null)
			return JSON.toJSONString(new ResultDto<Object>(GlobalVariable.PARAMS_CODE, GlobalVariable.PARAMS_MESSAGE)); 
		ResultDto<Integer> resultDto = singleWordService.insert(name, meaning);
		return JSON.toJSONString(resultDto); 
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/searchByName")
	public String searchByName(String name) {
		if (name == null) 
			return JSON.toJSONString(new ResultDto<Object>(GlobalVariable.PARAMS_CODE, GlobalVariable.PARAMS_MESSAGE)); 
		ResultDto<List<SingleWord>> resultDto = singleWordService.searchByNameL(name);
		
		return JSON.toJSONString(resultDto); 
	}
	
	@ResponseBody
	@RequestMapping(value = "/{id}/changeMeaning")
	public String changeMeaning(@PathVariable("id") Long id, String meaning) {
		if (meaning == null || id == null) 
			return JSON.toJSONString(new ResultDto<Object>(GlobalVariable.PARAMS_CODE, GlobalVariable.PARAMS_MESSAGE)); 
		ResultDto<Integer> resultDto = singleWordService.changeMeaning(id, meaning);
		return JSON.toJSONString(resultDto); 
	}
	
	@ResponseBody
	@RequestMapping(value = "/{id}/delete")
	public String changeMeaning(@PathVariable("id") Long id) {
		if (id == null) 
			return JSON.toJSONString(new ResultDto<Object>(GlobalVariable.PARAMS_CODE, GlobalVariable.PARAMS_MESSAGE)); 
		ResultDto<Integer> resultDto = singleWordService.deleteById(id);
		return JSON.toJSONString(resultDto); 
	}
	
	@ResponseBody
	@RequestMapping(value = "/searchByParams")
	public String searchByParams(String name) {
		ResultDto<List<SingleWord>> resultDto = singleWordService.searchByParams(name);
		return JSON.toJSONString(resultDto); 
	}
	
	@ResponseBody
	@RequestMapping(value = "/page/{pageIndex}/{pageSize}")
	public String searchByParams(String name, @PathVariable("pageIndex") Integer pageIndex, @PathVariable("pageSize") Integer pageSize) {
		ResultDto<List<SingleWord>> resultDto = singleWordService.pageByParams(pageIndex, pageSize, name);
		return JSON.toJSONString(resultDto); 
	}
	
	@ResponseBody
	@RequestMapping(value = "/pageData/{pageIndex}/{pageSize}")
	public String pageData(String name, @PathVariable("pageIndex") Integer pageIndex, @PathVariable("pageSize") Integer pageSize) {
		ResultDto<PageData> resultDto = singleWordService.getPageData(pageIndex, pageSize, name);
		return JSON.toJSONString(resultDto);  
	}
	
	
}
